"""
首页相关路由
"""

from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from typing import List, Dict, Any

from database import get_db
from models.common import StandardResponse
from models.app import HomeData, QuickAction
from services.auth_service import get_current_user
from services.home_service import HomeService

router = APIRouter(prefix="/home", tags=["首页"])

@router.get("/info", response_model=StandardResponse, summary="获取首页信息")
async def get_home_info(
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取首页信息接口
    
    返回首页所需的所有数据，包括：
    - 轮播图
    - 企业新闻/公告
    - 快速操作
    - 统计数据
    - 最近活动
    """
    try:
        home_service = HomeService(db)
        home_data = await home_service.get_home_data(current_user["id"])
        
        return StandardResponse(
            success=True,
            message="获取首页信息成功",
            data=home_data
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取首页信息失败: {str(e)}"
        )

@router.get("/banners", response_model=StandardResponse, summary="获取轮播图")
async def get_banners(
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取首页轮播图
    """
    try:
        home_service = HomeService(db)
        banners = await home_service.get_banners()
        
        return StandardResponse(
            success=True,
            message="获取轮播图成功",
            data=banners
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取轮播图失败: {str(e)}"
        )

@router.get("/news", response_model=StandardResponse, summary="获取企业新闻")
async def get_news(
    page: int = 1,
    page_size: int = 10,
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取企业新闻列表
    
    - **page**: 页码
    - **page_size**: 每页数量
    """
    try:
        home_service = HomeService(db)
        news_data = await home_service.get_news(page, page_size)
        
        return StandardResponse(
            success=True,
            message="获取企业新闻成功",
            data=news_data
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取企业新闻失败: {str(e)}"
        )

@router.get("/announcements", response_model=StandardResponse, summary="获取公告")
async def get_announcements(
    page: int = 1,
    page_size: int = 10,
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取公告列表
    
    - **page**: 页码
    - **page_size**: 每页数量
    """
    try:
        home_service = HomeService(db)
        announcements = await home_service.get_announcements(page, page_size)
        
        return StandardResponse(
            success=True,
            message="获取公告成功",
            data=announcements
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取公告失败: {str(e)}"
        )